package me.baicai.modules.belldandy.gcb.service.impl;

import me.baicai.modules.belldandy.gcb.domain.UserDividend;
import me.baicai.modules.belldandy.gcb.repository.UserDividendRepository;
import me.baicai.modules.belldandy.gcb.service.UserDividendService;
import me.baicai.modules.belldandy.gcb.service.dto.UserDividendDTO;
import me.baicai.modules.belldandy.gcb.service.dto.UserDividendQueryCriteria;
import me.baicai.modules.belldandy.gcb.service.mapper.UserDividendMapper;
import me.baicai.utils.FileUtil;
import me.baicai.utils.PageUtil;
import me.baicai.utils.QueryHelp;
import me.baicai.utils.ValidationUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author 超级大白菜
* @date 2020-06-02
*/
@Service
@CacheConfig(cacheNames = "userDividend")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserDividendServiceImpl implements UserDividendService {

    private final UserDividendRepository userDividendRepository;

    private final UserDividendMapper userDividendMapper;

    public UserDividendServiceImpl(UserDividendRepository userDividendRepository, UserDividendMapper userDividendMapper) {
        this.userDividendRepository = userDividendRepository;
        this.userDividendMapper = userDividendMapper;
    }

    @Override
    @Cacheable
    public Map<String,Object> queryAll(UserDividendQueryCriteria criteria, Pageable pageable){
        Page<UserDividend> page = userDividendRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(userDividendMapper::toDto));
    }

    @Override
    @Cacheable
    public List<UserDividendDTO> queryAll(UserDividendQueryCriteria criteria){
        return userDividendMapper.toDto(userDividendRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "#p0")
    public UserDividendDTO findById(Integer pid) {
        UserDividend userDividend = userDividendRepository.findById(pid).orElseGet(UserDividend::new);
        ValidationUtil.isNull(userDividend.getPid(),"UserDividend","pid",pid);
        return userDividendMapper.toDto(userDividend);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public UserDividendDTO create(UserDividend resources) {
        return userDividendMapper.toDto(userDividendRepository.save(resources));
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(UserDividend resources) {
        UserDividend userDividend = userDividendRepository.findById(resources.getPid()).orElseGet(UserDividend::new);
        ValidationUtil.isNull( userDividend.getPid(),"UserDividend","id",resources.getPid());
        userDividend.copy(resources);
        userDividendRepository.save(userDividend);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer pid) {
        userDividendRepository.deleteById(pid);
    }


    @Override
    public void download(List<UserDividendDTO> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserDividendDTO userDividend : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("提成百分比", userDividend.getCommission());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
